package com.bert.generator.base.db;

import java.math.BigDecimal;
import java.util.Date;

/**
 * jdbc枚举类
 */
public enum JdbcType {
    BIT,
    TINYINT,
    SMALLINT,
    INT, INTEGER,
    BIGINT,
    FLOAT,
    DOUBLE,
    DECIMAL,
    DATE,
    DATETIME,
    CHAR,
    VARCHAR,
    BOOLEAN,
    TEXT,
    JSON,
    MEDIUMTEXT,
    LONGTEXT,
    UNKNOWN;

    /**
     * 根据jdbc类型获取对应的java类型字符串
     *
     * @param jdbcType jdbcType
     * @return return
     */
    public static Class getJavaType(JdbcType jdbcType) {
        switch (jdbcType) {
            case CHAR:
                return Character.class;
            case BIT:
            case BOOLEAN:
                return Boolean.class;
            case FLOAT:
                return Float.class;
            case BIGINT:
                return Long.class;
            case DOUBLE:
                return Double.class;
            case DECIMAL:
                return BigDecimal.class;
            case DATETIME:
            case DATE:
                return Date.class;
            case TINYINT:
            case INTEGER:
            case SMALLINT:
            case INT:
                return Integer.class;
            case VARCHAR:
            case TEXT:
            case MEDIUMTEXT:
            case LONGTEXT:
            case JSON:
            case UNKNOWN:
            default:
                return String.class;
        }
    }

    /**
     * 获取全限定类名
     *
     * @param jdbcType jdbcType
     * @return
     */
    public static String getFullClassName(JdbcType jdbcType) {
        return getJavaType(jdbcType).getName();
    }

    /**
     * 获取简单类名
     *
     * @param jdbcType
     * @return
     */
    public static String getSimpleClassName(JdbcType jdbcType) {
        return getJavaType(jdbcType).getSimpleName();
    }

    /**
     * 将java中的类型转成枚举
     * {@link java.sql.Types}
     *
     * @return
     */
    public static JdbcType getJdbcType(int type) {
        switch (type) {
            case -7:
                return BIT;
            case -6:
                return TINYINT;
            case 5:
                return SMALLINT;
            case 4:
                return INTEGER;
            case -5:
                return BIGINT;
            case 6:
                return FLOAT;
            case 8:
                return DOUBLE;
            case 1:
                return CHAR;
            case 12:
                return VARCHAR;
            case 16:
                return BOOLEAN;
            default:
                throw new RuntimeException("未支持的jdbc类型：" + type);
        }
    }


}
